package com.doeis.uwork.dao;

import java.util.ArrayList;
import java.util.List;

import com.doeis.common.utils.StringUtils;
import com.doeis.uwork.common.Constant;
import com.doeis.uwork.entity.Share;
import com.google.inject.Singleton;
import com.voland.core.db.Dao;
import com.voland.core.db.DaoException;
import com.voland.core.db.Page;

@Singleton
public class ShareDao extends Dao {

	public Share findShareByid(Object id) throws DaoException{
		try{
			return super.readById(Share.class, id);
		}catch (Exception e) {
			throw new DaoException(e);
		}
	}
	
	
	public Share findShare(Share shareForm) throws DaoException{
		try{
			List<Object> params = new ArrayList<Object>();
			StringBuffer sql = new StringBuffer("select * from share where true ");
			if (!StringUtils.isBlank(shareForm.getUser_id())) {
				sql.append(" and user_id=? ");
				params.add(shareForm.getUser_id());
			}
			if (shareForm.getSource_type()!=null) {
				sql.append(" and source_type=? ");
				params.add(shareForm.getSource_type());
			}
			if( !StringUtils.isBlank(shareForm.getSource_id()) ){
				sql.append(" and source_id=? ");
				params.add(shareForm.getSource_id());
			}
			if (shareForm.getItem_id()!=null) {
				sql.append(" and item_id = ? ");
				params.add(shareForm.getItem_id());
			}
			if (shareForm.getScope()!=null) {
				sql.append(" and scope = ? ");
				params.add(shareForm.getScope());
			}
			sql.append(" order by updated_at desc ");
			return super.read(Share.class, sql.toString(), params.toArray());
		}catch (Exception e) {
			throw new DaoException(e);
		}
	}

	public Page<Share> listShares(Page page,Share shareForm, String user_id) throws DaoException{
		try{
			List<Object> params = new ArrayList<Object>();
			StringBuffer sql = new StringBuffer("select * from share where true ");
			if (!StringUtils.isBlank(user_id)) {
				sql.append(" and user_id=? ");
				params.add(user_id);
			}
			if (shareForm!=null) {
				if (shareForm.getSource_type()!=null) {
					sql.append(" and source_type=? ");
					params.add(shareForm.getSource_type());
				}
				if( !StringUtils.isBlank(shareForm.getSource_id()) ){
					sql.append(" and source_id=? ");
					params.add(shareForm.getSource_id());
				}
				if (shareForm.getDescrip()!=null) {
					sql.append(" and descrip like ? ");
					params.add("%"+shareForm.getDescrip()+"%");
				}
				if (shareForm.getName()!=null) {
					sql.append(" and name like ? ");
					params.add("%"+shareForm.getName()+"%");
				}
				if(shareForm.getEmails() != null){
					sql.append(" and emails like ?");
					params.add("%"+shareForm.getEmails()+"%");
				}
			}
			sql.append(" order by updated_at desc ");
			return super.querySlice(Share.class, sql.toString(), page,params.toArray());
		}catch (Exception e) {
			throw new DaoException(e);
		}
	}

	/**
	 * 
	 * @param shareForm
	 * @return
	 * @author wxy
	 * @date 2014年9月2日 下午4:39:45
	 */
	public List<Share> findShareList(Share shareForm) {
		List<Object> params = new ArrayList<Object>();
		StringBuffer sql = new StringBuffer("select * from share where true ");
		
		if(!StringUtils.isBlank(shareForm.getUser_id())){
			sql.append(" and user_id = ?");
			params.add(shareForm.getUser_id());
		}
		if (shareForm.getSource_type()!=null) {
			sql.append(" and source_type=? ");
			params.add(shareForm.getSource_type());
		}
		/*if (shareForm.getStatus()!=null) {
			if (shareForm.getScope()!=null) {
				sql.append(" and (scope=? or scope=?) ");
				params.add(shareForm.getScope());
				params.add(shareForm.getStatus());
			}
		}else {
			if (shareForm.getScope()!=null) {
				sql.append(" and scope=? ");
				params.add(shareForm.getScope());
			}
		}*/
		if (shareForm.getScope()!=null) {
			sql.append(" and scope=? ");
			params.add(shareForm.getScope());
		}
		
		if( !StringUtils.isBlank(shareForm.getSource_id()) ){
			sql.append(" and source_id=? ");
			params.add(shareForm.getSource_id());
		}
		if( !StringUtils.isBlank(shareForm.getItem_id()) ){
			sql.append(" and item_id=? ");
			params.add(shareForm.getItem_id());
		}
		if(shareForm.getEmails() != null){
			sql.append(" and emails like ?");
			params.add("%"+shareForm.getEmails()+"%");
		}
		if (shareForm.getUpdated_at()!=null) {
			sql.append(" and updated_at<?");
			params.add(shareForm.getUpdated_at());
		}
		return super.query(Share.class, sql.toString(), params.toArray());
	}

	public List<Share> findShareFDList(String user_id) {
		List<Object> params = new ArrayList<Object>();
		StringBuffer sql = new StringBuffer("select * from share where true and user_id=? and (scope=? or scope=?)");
		params.add(user_id);
		params.add(Constant.SHARE_SCOPE_ISP);
		params.add(Constant.SHARE_SCOPE_VIP);
		return super.query(Share.class, sql.toString(), params.toArray());
	}
	
}